$(document).ready(function(){
	getSchool();
	function getSchool(){
		$.ajax({
	        url:"../queryAllSchool",
	        type:"post",
	        success:function (data) {
	            if(data.code===100){
	                var schools=data.extend;
	                console.log(schools);
	                makeSchoolSelection(data);
	            }

	        }
	    });
	}
	 
	 function makeSchoolSelection(data) {
	        $("#school").empty();
	        var option=$("<option></option>").append("请选择学校").val(0);
	        option.appendTo("#school")
	        $.each(data.extend.schools,function(index,item){
	            var option=$("<option></option>").append(item.schoolname+"&nbsp;&nbsp;&nbsp;&nbsp;代码："+item.code).val(item.schoolid);
	            option.appendTo("#school");
	        });
	    }
	 
	 $("#addSchool").click(function(){
		 $.ajax({
			 url:"../addSchool",
			 data:$(".addSchoolForm").serialize(),
			 type:"POST",
			 success:function(data){
				 $("#addSchoolModal").modal("hide");
				 $(".addSchoolForm")[0].reset();
				 if(data.code==100){
					 reminder("success","添加成功");
					 getSchool();
				 }else{
					 reminder("error","添加失败，学校代码重复");
				 }
			 }
		 })
	 })
	 
	 
	 
	 
	 $("#submitClass").click(function(){
		 var schoolId=$("#school option:selected").val();
		 var className=$("#clazz").val();
		 
		 var location=$("#file").val();
		
			var point = location.lastIndexOf("."); 
		    var type = location.substr(point); 
		    if(type==".xlsx"||type==".xls"){
		    	var formData=new FormData();
				var obj=$('#file')[0].files[0];
				formData.append("file",obj);
				formData.append("className",className);
				formData.append("schoolId",schoolId);
		    	$.ajax({
		            url: '../addClass',  //这里是服务器处理的代码
		            data:formData, //传递参数到服务器
		            type: 'POST',
		            processData:false,
		            contentType:false,
		            beforeSend:function(){
		            	 $("#submitClass").attr("disabled",true);
		            },
		            success:function(data){
		            	$("#submitClass").attr("disabled","false");
		            	if(data.code==100){
		            		reminder("success","创建成功");
		            	}else{
		            		reminder("error","创建失败");
		            	}
		            }
		        });
		    }else{
		    	reminder("error","请完善信息，文件格式必须为xlsl");
		    	return false;
		    }
	 });
	//提示信息
		function reminder(Status,msg) {
			swal({
				  title: msg,
				  text: "学生考勤系统",
				  timer: 1500,
				  type:Status,
				  showConfirmButton: false
				});
		}	
})